//
//  ViewController.swift
//  闭包滚动视图
//
//  Created by tang on 15/12/15.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let asd = CGRectMake(0, 20, 375, 30)
        
        let svas = scrollview(asd, numberlable: { () -> Int in
            return 12
            }) { (index) -> UILabel in
                // 创建一个 lable
                let l = UILabel()
                
                l.text = "hello \(index)"
                
                l.textAlignment = NSTextAlignment.Center
                l.font = UIFont.systemFontOfSize(18)
                // 根据内容自动调整大小
                l.sizeToFit()
                
                l.font = UIFont.systemFontOfSize(14)
                
                return l
        }
        view.addSubview(svas)
    }

    
    
    
    func scrollview(frame: CGRect, numberlable:() -> Int,  lableWithIndex: (index: Int) -> UILabel ) -> UIScrollView{
    
    //实例化一个sv
        
        let sv = UIScrollView(frame: frame)
        sv.backgroundColor = UIColor.brownColor()
        
        // 知道 lable 的个数
        let count = numberlable()
        print(count)
        
        //每一个lable显示什么内容
        
        let magein: CGFloat = 8
        var x = magein
        
        for i in 0..<count{
        let lable = lableWithIndex(index: i)
            
            lable.frame = CGRectMake(x, 0, lable.bounds.width, frame.height)
            sv.addSubview(lable)
            x += lable.bounds.width
            
        }
        sv.contentSize = CGSizeMake(x + magein,frame.height)
        
        
        return sv
    
    }


}

